<?php

/**
 * @author kevin
 * @date 2016-01-16 14:00:00
 * @desc 首页广告
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$version = '1.0';   //版本号

if ($_POST['api_version'] != $version)      //网店的接口版本低
{
	api_err('0x008', 'a low version api');
}
if(empty($_POST['st'])){
	api_err('0x008', 'param st is Missing');
}
//商品详情
$goods_id = isset($_POST['goods_id']) ? intval($_POST['goods_id']) : 0;
if($_POST['st'] == 'detail'){
	if(empty($goods_id)){
		api_err('0x008', 'param goods_id can not empty');
	}
	//详情
	$data['goods'] = get_goods_infov1($goods_id);
	//图文详情处理
	$list = json_decode($data['goods']['goods_desc'], true);
	$goods_desc = array();
	if(!empty($list)){
		foreach($list as $value){
			$goods_desc[] =  get_image_pathv1('',$value['img_url']);
		}
	}
	$data['goods']['goods_desc'] = $goods_desc;
    $data['goods']['goods_size_url'] = get_image_pathv1('','wap/goodsize.php?id='.$goods_id);
	//是否已收藏
	$data['goods']['is_collection'] = 0;
	$user_id = $GLOBALS['uid'];
	if(!empty($user_id)){
		$sql = "SELECT COUNT(*) FROM " .$GLOBALS['ecs']->table('collect_goods') .
			" WHERE user_id='$user_id' AND goods_id = '$goods_id'";
		if ($GLOBALS['db']->GetOne($sql) > 0){
			$data['goods']['is_collection'] = 1;
		}
	}
	//商品水印
	$data['goods']['watermark_img'] =  get_water_img($goods_id,$data['goods']['is_new'],$data['goods']['promote_price']);
	$gift = get_goods_gift($goods_id);
	$data['goods']['is_gift'] = empty($gift)?0:1;
	//相册
	$data['gallery'] = get_goods_galleryv1($goods_id);
	//相同属性的关联商品
	$data['relation'] = get_linked_goods($goods_id);
	//商品评价
	$data['comments'] = assign_commentv1($goods_id, 0, 1);
	if(!empty($data['comments'])){
		$data['comments'] = $data['comments'][0];
	}else{
		$data['comments'] = null;
	}
	/* 更新点击次数 */
	$GLOBALS['db']->query('UPDATE ' . $GLOBALS['ecs']->table('goods') . " SET click_count = click_count + 1 WHERE goods_id = '$goods_id'");
}
//赠品接口
if($_POST['st'] == 'gift'){
	if(empty($goods_id)){
		api_err('0x008', 'param goods_id can not empty');
	}
	$data = get_goods_gift($goods_id);
}
//st参数错误
if(!isset($data)){
	api_err('0x008');
}
//返回数据
data_back($data, '', RETURN_TYPE);

/**
 * 获得指定商品的关联商品
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_linked_goods($goods_id)
{
    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price AS org_price, ' .
                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
                'g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date ' .
            'FROM ' . $GLOBALS['ecs']->table('link_goods') . ' lg ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = lg.link_goods_id ' .
            "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
                    "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
            "WHERE lg.goods_id = '$goods_id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
            "LIMIT " . $GLOBALS['_CFG']['related_goods_number'];
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
	$key = 0;
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$key]['goods_id']     = $row['goods_id'];
        $arr[$key]['goods_name']   = $row['goods_name'];
        $arr[$key]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
            sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        $arr[$key]['goods_thumb']  = get_image_pathv1($row['goods_id'], $row['goods_thumb'], true);
        $arr[$key]['goods_img']    = get_image_pathv1($row['goods_id'], $row['goods_img']);
        $arr[$key]['market_price'] = $row['market_price'];//price_format($row['market_price']);
        $arr[$key]['shop_price']   = $row['shop_price'];//price_format($row['shop_price']);
		$arr[$key]['watermark_img'] =  get_water_img($goods_id,$row['is_new'],$row['promote_price']);
        if ($row['promote_price'] > 0)
        {
            $arr[$key]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $arr[$key]['promote_price'] = 0;
        }
		$key++;
    }

    return $arr;
}
